package com.shenyi.shop.service;

import com.shenyi.core.utils.Base64;
import com.shenyi.shop.api.ICommentService;
import com.shenyi.shop.dao.CommentMapper;
import com.shenyi.shop.dao.CommentPictureMapper;
import com.shenyi.shop.entity.CommentDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author richard.fan
 * @version 1.0
 * @date 2018/11/05
 * @desc 评论
 * @see
 * @since 1.0
 */
@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentPictureMapper commentPictureMapper;

    @Override
    public CommentDO queryObject(Integer id) {
        return commentMapper.queryObject(id);
    }

    @Override
    public List<CommentDO> queryList(Map<String, Object> map) {
        List<CommentDO> commentEntities = commentMapper.queryList(map);
        if (null != commentEntities && commentEntities.size() > 0) {
            for (CommentDO CommentDO : commentEntities) {
                CommentDO.setContent(Base64.decode(CommentDO.getContent()));
            }
        }
        return commentEntities;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return commentMapper.queryTotal(map);
    }

    @Override
    public int save(CommentDO comment) {
        return commentMapper.save(comment);
    }

    @Override
    public int update(CommentDO comment) {
        return commentMapper.update(comment);
    }

    @Override
    @Transactional
    public int delete(Integer id) {
        //删除评论同时删除评论的图片
        commentPictureMapper.deleteByCommentId(id);
        return commentMapper.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return commentMapper.deleteBatch(ids);
    }

    @Override
    public int toggleStatus(CommentDO comment) {
        CommentDO CommentDO = queryObject(comment.getId());
        if (CommentDO.getStatus() == 1) {
            CommentDO.setStatus(0);
        } else if (CommentDO.getStatus() == 0) {
            CommentDO.setStatus(1);
        }
        return commentMapper.update(CommentDO);
    }
}
